package com.zzg.redis.util;

import org.springframework.util.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPool {
	private JedisPoolConfig config; 
    private String serverIp;
    private String password;
    private int port;
    private int timeout;
    private JedisPool pool;

    public void init() {
        pool = new JedisPool(config, serverIp, port, timeout);
    }

    public Jedis getInstance() {
    	Jedis jedis = pool.getResource();
    	if(!StringUtils.isEmpty(password)){
    		jedis.auth(password);
    	}
    	return jedis;
    }

    public void returnResource(Jedis jedis) {
//        pool.returnResource(jedis);
    	if (jedis != null) {
            jedis.close();
        }
    }

    public void returnBrokenResource(Jedis jedis){
    	if (jedis != null) {
            jedis.close();
        }
    }

    public JedisPoolConfig getConfig() {
        return config;
    }

    public void setConfig(JedisPoolConfig config) {
        this.config = config;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
